const Keys = require('../template')

console.log("=== Keys 性能测试 ===")

// 测试小对象键获取性能
console.log(
  "小对象键获取性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 1000000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keys(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.001 // 平均每次获取时间应该小于0.001ms
  })()
)

// 测试大对象键获取性能
console.log(
  "大对象键获取性能:", (() => {
    const obj = {}
    for (let i = 0; i < 10000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keys(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次获取时间应该小于0.1ms
  })()
)

// 测试深度获取键性能
console.log(
  "深度获取键性能:", (() => {
    const obj = { a: { b: { c: { d: { e: 1 } } } } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysDeep(obj, 5)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试条件过滤键性能
console.log(
  "条件过滤键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysWithFilter(obj, (value, key) => value > 500)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次获取时间应该小于0.1ms
  })()
)

// 测试类型过滤键性能
console.log(
  "类型过滤键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i % 2 === 0 ? i : `string${i}`
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysWithType(obj, 'number')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次获取时间应该小于0.1ms
  })()
)

// 测试正则过滤键性能
console.log(
  "正则过滤键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`user_${i}`] = i
      obj[`admin_${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysWithPattern(obj, /^user_/)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次获取时间应该小于0.1ms
  })()
)

// 测试排序键性能
console.log(
  "排序键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${Math.random()}`] = i
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysWithSort(obj, (a, b) => a.localeCompare(b))
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次获取时间应该小于1ms
  })()
)

// 测试转换键性能
console.log(
  "转换键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysWithTransform(obj, key => key.toUpperCase())
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次获取时间应该小于0.1ms
  })()
)

// 测试验证键性能
console.log(
  "验证键性能:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = { name: { type: 'string' }, age: { type: 'number' }, email: { type: 'string' } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        Keys.keysWithValidation(obj, schema)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试统计键性能
console.log(
  "统计键性能:", (() => {
    const obj = { a: 1, b: { c: 2 }, d: [3, 4] }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keysWithStats(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试异步键性能
console.log(
  "异步键性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const iterations = 1000
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(Keys.keysAsync(obj, async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value > 2
      }))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => Array.isArray(result)) &&
        avgTime < 5 // 平均每次获取时间应该小于5ms
    }).catch(() => false)
  })()
)

// 测试并发键性能
console.log(
  "并发键性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const iterations = 100
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(Keys.keysConcurrent(obj, async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value > 2
      }, 3))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => Array.isArray(result)) &&
        avgTime < 10 // 平均每次获取时间应该小于10ms
    }).catch(() => false)
  })()
)

// 测试复杂结构键性能
console.log(
  "复杂结构键性能:", (() => {
    const obj = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}`, age: 20 + i },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          likes: j * 10
        }))
      })),
      meta: { total: 100, page: 1 }
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Keys.keys(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试内存使用性能
console.log(
  "内存使用性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 10000
    const results = []

    for (let i = 0; i < iterations; i++) {
      const keys = Keys.keys(obj)
      results.push(keys)
    }

    return results.length === iterations &&
      results.every(result => Array.isArray(result) && result.length === 3)
  })()
)

console.log("=== Keys 性能测试完成 ===")
